package com.ikingtech.platform.service.system.variable.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.cache.constants.CacheConstants;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.variable.model.VariableQueryParamDTO;
import com.ikingtech.platform.service.system.variable.entity.VariableDO;
import com.ikingtech.platform.service.system.variable.mapper.VariableMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tie yan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VariableService extends ServiceImpl<VariableMapper, VariableDO> {

    private final StringRedisTemplate redisTemplate;

    public List<VariableDO> listByTenantCode(String tenantCode) {
        return this.list(Wrappers.<VariableDO>query().lambda().eq(VariableDO::getTenantCode, tenantCode).orderByDesc(VariableDO::getCreateTime));
    }

    public String getValueByKey(String key) {
        return this.getObj(Wrappers.<VariableDO>lambdaQuery()
                .select(VariableDO::getVariableValue)
                .eq(VariableDO::getVariableKey, key)
                .eq(VariableDO::getTenantCode, Me.tenantCode()), String.class::cast);
    }

    public void syncVariableToCache(VariableDO entity) {
        if (null == entity) {
            return;
        }
        this.redisTemplate.opsForHash().put(CacheConstants.systemVariableFormat(entity.getTenantCode()), entity.getVariableKey(), entity.getVariableValue());
    }

    public void syncVariableToCache(List<VariableDO> entities) {
        if (Tools.Coll.isBlank(entities)) {
            return;
        }
        Tools.Coll.convertGroup(entities, VariableDO::getTenantCode).forEach((tenantCode, groupedEntities) -> this.redisTemplate.opsForHash().putAll(CacheConstants.systemVariableFormat(tenantCode), Tools.Coll.convertMap(groupedEntities, VariableDO::getVariableKey, VariableDO::getVariableValue)));
    }

    public Boolean nameExist(String name, String tenantCode) {
        return this.baseMapper.exists(Wrappers.<VariableDO>lambdaQuery()
                .isNotNull(VariableDO::getId)
                .eq(VariableDO::getName, name)
                .eq(VariableDO::getTenantCode, tenantCode));
    }

    public Boolean nameExist(String id, String name, String tenantCode) {
        return this.baseMapper.exists(Wrappers.<VariableDO>lambdaQuery()
                .ne(VariableDO::getId, id)
                .eq(VariableDO::getName, name)
                .eq(VariableDO::getTenantCode, tenantCode));
    }

    public PageResult<VariableDO> listPage(VariableQueryParamDTO queryParam, String tenantCode) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<VariableDO>lambdaQuery()
                .like(Tools.Str.isNotBlank(queryParam.getName()), VariableDO::getName, queryParam.getName())
                .like(Tools.Str.isNotBlank(queryParam.getVariableKey()), VariableDO::getVariableKey, queryParam.getVariableKey())
                .like(Tools.Str.isNotBlank(queryParam.getVariableValue()), VariableDO::getVariableValue, queryParam.getVariableValue())
                .eq(Tools.Str.isNotBlank(queryParam.getType()), VariableDO::getType, queryParam.getType())
                .eq(VariableDO::getTenantCode, tenantCode)
                .orderByDesc(VariableDO::getCreateTime)));
    }

    public void removeByTenantCode(String tenantCode) {
        this.remove(Wrappers.<VariableDO>lambdaQuery().eq(VariableDO::getTenantCode, tenantCode));
    }
}
